package com.wjk.kylin.core.common.oss.ali;

import cn.hutool.core.util.IdUtil;
import com.aliyun.oss.OSS;
import com.wjk.kylin.config.properties.AliCloudOSSProperties;
import com.wjk.kylin.core.common.oss.AbstractOSSServer;
import com.wjk.kylin.core.common.oss.OSSFileModel;
import com.wjk.kylin.core.exception.BusinessException;
import com.wjk.kylin.core.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

@Slf4j
@Component
public class AliCloudServer extends AbstractOSSServer<OSSFileModel> {

    @Autowired
    private OSS ossClient;

    @Override
    protected String upload(OSSFileModel model) {

        String path = AliCloudOSSProperties.PREFIX + File.separator + DateUtils.formatDate("yyyy/MM/dd") + File.separator + IdUtil.simpleUUID() + "." + model.getExtName();

        try {
            ossClient.putObject(AliCloudOSSProperties.BUCKET_NAME, path, model.getFile().getInputStream());
        } catch (Exception e){
            log.error("上传文件失败,path:{}",path,e);
            throw new BusinessException("上传文件失败，请检查配置信息");
        } finally {
            // 关闭OSSClient。
            ossClient.shutdown();
        }

        return AliCloudOSSProperties.DOMAIN + File.separator + path;

    }

    @Override
    protected void delete(String filePath) {

    }

    @Override
    protected File download(String filePath) {
        return null;
    }

}
